package com.ruoyi.user.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.request.PageRequest;
import com.ruoyi.common.core.exception.auth.NotLoginException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresLogin;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.user.domain.entity.User;
import com.ruoyi.user.domain.request.LoginRequest;
import com.ruoyi.user.domain.vo.LoginVo;
import com.ruoyi.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.core.web.domain.AjaxResult.success;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xyf
 * @since 2024-12-16
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;


    @PostMapping("/login")
    public R<LoginVo> loginTest(@Validated @RequestBody LoginRequest loginRequest){
        if(StrUtil.isBlank(loginRequest.getUserName()) || StrUtil.isBlank(loginRequest.getPassword())){
            throw new NotLoginException("账号密码不能为空");
        }
        return R.ok(userService.login(loginRequest));
    }

    @RequiresLogin
    @GetMapping("/getUserInfoById")
    public R<User> getUserInfoById(@RequestParam Integer userId){
        User user = userService.lambdaQuery().eq(User::getId, userId).one();
        if(user==null){
            throw new RuntimeException("没有查询到此用户");
        }
        return R.ok(user);
    }


    /**
     * 查询用户列表
     */
    @RequiresPermissions("user:user:list")
    @GetMapping("/list")
    public TableDataInfo list(PageRequest pageRequest) {
        Page<User> page = userService.page(new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize()));
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("user:user:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageRequest pageRequest)
    {
        Page<User> page = userService.page(new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize()));
        List<User> list = page.getRecords();
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @RequiresPermissions("user:user:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        return success(user);
    }

    /**
     * 新增用户
     */
    @RequiresPermissions("user:user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user) {
        user.setCreateTime(new Date());
        boolean isSuccess = userService.save(user);
        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改用户
     */
    @RequiresPermissions("user:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user) {
        boolean isSuccess = userService.updateById(user);
        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("user:user:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        boolean isSuccess = userService.removeBatchByIds(idList);
        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }

}
